from dataclasses import asdict

from odoo.api import Environment
from odoo.addons.fastapi.dependencies import odoo_env
from ..exception.customer_exception import CustomerException
from ..models.enums.app_http_code_enum import AppHttpCodeEnum
from ..models.vo.coupon_related_vo import CouponRelatedVo

from ..tests.case_collection.test_security_case import TestSecurityCase
from ..tests.case_collection.test_coupon_case import TestCouponTestCase
from fastapi import APIRouter, Depends

router = APIRouter(prefix='/coupon', tags=['优惠券'])


@router.post('/direct_pay_route',
             summary='Coupon Testing Route One',
             description='点单页直接点单，会员登录',
             response_model=dict)
async def coupon_testing_route_one(coupon_related_vo: CouponRelatedVo, env: Environment = Depends(odoo_env)):
    response = TestCouponTestCase().pos_order_direct_route(coupon_name=coupon_related_vo.coupon_name, goods_list=coupon_related_vo.goods_list,
                                                           path=coupon_related_vo.path, pay_detail=coupon_related_vo.pay_detail, discount_way=coupon_related_vo.discount_way,
                                                           vip_search=coupon_related_vo.vip_search, odoo_env=env, odoo_record=coupon_related_vo.odoo_record)
    if response.code == AppHttpCodeEnum.CASE_ERROR_RECOVER_FILE.code:
        # 恢复现场失败那么去重新启动，不带缓存
        response = TestSecurityCase().login_accord_applet_id_clerk_num_route2(language=coupon_related_vo.language, applet_id=coupon_related_vo.applet_id,
                                                                              applet_pwd=coupon_related_vo.applet_pwd, clerk_name=coupon_related_vo.clerk_id, clerk_num=coupon_related_vo.clerk_num,
                                                                              env=coupon_related_vo.env)
        response.error_message='优惠券测试时发生异常并重启应用时，'+response.error_message
    return asdict(response)


@router.post('/behind_pay_route',
             summary='Coupon Testing Route Two',
             description='点单页后付，订单页进行会员登录',
             response_model=dict)
async def coupon_testing_route_two(coupon_related_vo: CouponRelatedVo, env: Environment = Depends(odoo_env)):
    response = TestCouponTestCase().pos_order_behind_route(coupon_name=coupon_related_vo.coupon_name, goods_list=coupon_related_vo.goods_list,
                                                           path=coupon_related_vo.path, pay_detail=coupon_related_vo.pay_detail, discount_way=coupon_related_vo.discount_way,
                                                           vip_search=coupon_related_vo.vip_search, odoo_env=env, odoo_record=coupon_related_vo.odoo_record)
    if response.code == AppHttpCodeEnum.CASE_ERROR_RECOVER_FILE.code:
        # 恢复现场失败那么去重新启动，不带缓存
        response = TestSecurityCase().login_accord_applet_id_clerk_num_route2(language=coupon_related_vo.language, applet_id=coupon_related_vo.applet_id,
                                                                              applet_pwd=coupon_related_vo.applet_pwd, clerk_name=coupon_related_vo.clerk_id, clerk_num=coupon_related_vo.clerk_num,
                                                                              env=coupon_related_vo.env)
    return asdict(response)
